package com.example.demo.main;


import com.example.demo.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class ExcelWriter {
    public static void main(String[] args) throws IOException {
        String filePath = "D:/Users/zyc/Desktop/fsdownload/工作簿1.xlsx";
        String outPutPath = String.format("D:/Users/zyc/Desktop/fsdownload/工作簿1_%d.xlsx", System.currentTimeMillis());

        Workbook workbook = createWorkbook(true);
        Sheet sheet1 = workbook.createSheet("Sheet1");


        Row row0 = sheet1.createRow(0);
        Cell cell00 = row0.createCell(0, CellType.BLANK);
        CellStyle cellStyle = new XSSFCellStyle(new StylesTable());
//        cell00.setCellStyle(cellStyle);// 设置单元格样式
        cell00.setCellValue("课程名称/人数/调查内容");



        workbook.write(new FileOutputStream(outPutPath));
    }

    private static Workbook createWorkbook(boolean xlsx) {
        return xlsx ? new XSSFWorkbook() : new HSSFWorkbook();
    }

    private static void test1(String outPutPath) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFCellStyle style = workbook.createCellStyle();
//        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);


        HSSFSheet sheet = workbook.createSheet("sheet");

        HSSFRow row0 = sheet.createRow(0);
        HSSFCell cell_00 = row0.createCell(0);
        cell_00.setCellStyle(style);
        cell_00.setCellValue("日期");
        HSSFCell cell_01 = row0.createCell(1);
        cell_01.setCellStyle(style);
        cell_01.setCellValue("午别");

        HSSFRow row1 = sheet.createRow(1);
        HSSFCell cell_10 = row1.createCell(0);
        cell_10.setCellStyle(style);
        cell_10.setCellValue("20180412");
        HSSFCell cell_11 = row1.createCell(1);
        cell_11.setCellStyle(style);
        cell_11.setCellValue("上午");

        HSSFRow row2 = sheet.createRow(2);
        HSSFCell cell_21 = row2.createCell(1);
        cell_21.setCellStyle(style);
        cell_21.setCellValue("下午");

        // 合并日期占两行(4个参数，分别为起始行，结束行，起始列，结束列)
        // 行和列都是从0开始计数，且起始结束都会合并
        // 这里是合并excel中日期的两行为一行
        CellRangeAddress region = new CellRangeAddress(1, 2, 0, 0);
        sheet.addMergedRegion(region);

        File file = new File(outPutPath);
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
        out.close();
    }


    private void createRow(int line, HSSFSheet sheet) {
        // 创建line行

    }
}

